import { Injectable } from '@nestjs/common';
import CreateRoleDto from '../dto/CreateRoleDto';
import { InjectRepository } from '@nestjs/typeorm';
import { In } from 'typeorm';
import RoleRepository from '../repository/role.repository';

@Injectable()
export default class RoleService {
  constructor(
    @InjectRepository(RoleRepository)
    private roleRepoitory: RoleRepository,
  ) {}

  async getRoleByRoleIds(roleIds: string[]) {
    return await this.roleRepoitory.find({
      where: { id: In(roleIds) },
    });
  }

  async create(role: CreateRoleDto) {
    const roleEntity = await this.roleRepoitory.save(
      this.roleRepoitory.create({
        keyword: role.keyword,
        name: role.name,
      }),
    );
    return roleEntity.id;
  }

  async getRoleByKey(roleKey: string) {
    return await this.roleRepoitory.findOne({
      where: { keyword: roleKey },
    });
  }

  async getRoleById(roleId: string) {
    return await this.roleRepoitory.findOne({
      where: { id: roleId },
    });
  }
}
